/**
 * Created by Administrator on 2017/3/21.
 */
var express = require('express');
var router = express.Router();
var NoteDao = require("../my_modules/NoteDao");
var events = require("events");
/**
 * 添加纸条内容
 */
router.post('/', function(req, res) {
    var emitter = new events.EventEmitter();
    emitter.on("addNoteSuccess" ,function (data) {
        res.send(data);
        res.end();
    });
    emitter.on("addNoteError",function (data) {
        res.send("error");
        res.end();
    });
    var params = req.body;
    ipaddress = req.connection.remoteAddress;
     param = [
         html_encode(params.notecontent),
         params.bgcolor,
         params.fontsize,
         params.fontsize,
         ipaddress
     ]
    var noteDao = new NoteDao();
    noteDao.insertNote(param,callback);
    //回调函数
    function callback(emitLabel,data) {
        emitter.emit(emitLabel,data);
    }
});

function html_encode(str)
{
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&gt;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\n/g, "<br>");
    return s;
}

module.exports = router;